package com.banyuan.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Random;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String phoneNum = req.getParameter("phoneNum");
        HttpSession session = req.getSession();
        String random = getRandom(6);
        session.setAttribute(phoneNum,random);
        System.out.println(phoneNum + "  收到的验证码是：" + random);
        resp.sendRedirect(req.getContextPath()+"/denglu.html");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }

    public String getRandom(int i) {
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i1 = 0; i1 < i; i1++) {
            int num = random.nextInt(10);
            sb.append(num);
        }
        return sb.toString();
    }
}
